import {
  createRouter,
  createWebHashHistory,
} from "vue-router";
import store from "@/store/store";
import { ElMessage } from "element-plus";
import { version } from "core-js";
const routes = [
  {
    path: "/",
    redirect: "/jxtc/home",
  },
  {
    path: "/login",
    name: "login",
    component: () => import("@/views/login/login.vue"),
    meta: {
      withoutLogin: true,       // 不需要登录、权限
      title: '登录',
    },
  },
  /* {
    path: "/error404",
    name: "error404",
    meta: {
      withoutLogin: true,       // 不需要登录、权限
      title: 'error',
    },
    component: () => import("@/views/error/error404.vue"),
  }, */
  {
    path: "/jxtc", // 首页
    name: "jxtc",
    redirect: "/jxtc/home",
    component: () => import("@/views/main.vue"),
    children: [
      {
        // 首页
        path: "/jxtc/home",
        name: "home",
        meta: {
          title: '概览',
        },
        component: () => import("@/views/home/home.vue"),
      },
      {
        // 站长管理
        path: "/jxtc/stationmaster",
        name: "stationmaster",
        meta: {
          title: '站长管理',
        },
        component: () => import("@/views/m_site/stationmaster/stationmaster.vue"),
      },
      {
        // 站点管理
        path: "/jxtc/site",
        name: "site",
        meta: {
          title: '站点管理',
        },
        component: () => import("@/views/m_site/site/site.vue"),
      },
      {
        // 店铺管理
        path: "/jxtc/store",
        name: "store",
        meta: {
          title: '店铺管理',
        },
        component: () => import("@/views/m_store/store/store.vue"),
      },
      {
        // 菜品
        path: "/jxtc/food",
        name: "food",
        meta: {
          title: '菜品管理',
        },
        component: () => import("@/views/m_store/food/food.vue"),
      },
      {
        // 优惠券
        path: "/jxtc/coupon",
        name: "coupon",
        meta: {
          title: '优惠券',
        },
        component: () => import("@/views/m_marketing/coupon/coupon.vue"),
      },
      {
        // 订单列表
        path: "/jxtc/orderList",
        name: "orderList",
        meta: {
          title: '订单列表',
        },
        component: () => import("@/views/order/index.vue"),
      },
      {
        // 订单列表
        path: "/jxtc/salesOrderList",
        name: "salesOrderList",
        meta: {
          title: '售后订单',
        },
        component: () => import("@/views/order/sales/index.vue"),
      },
      {
        // 订单列表
        path: "/jxtc/foodList",
        name: "foodList",
        meta: {
          title: '菜品份数表',
        },
        component: () => import("@/views/order/foodSummary/index.vue"),
      },
      {
        path:'/jxtc/revenue',
        name:'revenue',
        meta:{
          title:'财务管理'
        },
        component:()=>import('@/views/finance/revenue/index.vue')
      },
      {
        path:'/jxtc/revenueStore',
        name:'revenueStore',
        meta:{
          title:'店铺财务'
        },
        component:()=>import('@/views/finance/store/index.vue')
      },
      {
        path:'/jxtc/revenueWages',
        name:'revenueWages',
        meta:{
          title:'站长及配送工资'
        },
        component:()=>import('@/views/finance/wages/index.vue')
      },
      // {
      //   // 菜品汇总
      //   path: "/jxtc/orderedFood",
      //   name: "orderedFood",
      //   meta: {
      //     title: '菜品汇总',
      //   },
      //   component: () => import("@/views/m_order/orderedFood/orderedFood.vue"),
      // },


    ],
  },
];
const router = createRouter({
  history: createWebHashHistory(),
  routes,
});


router.beforeEach((to, from, next) => {
  let allRoute = store.state.allRoute;
  if (to.meta.withoutLogin || location.origin.includes('localhost---')) {
    toNext();
  } else if(allRoute.length) {
    checkNext();
  } else {
    store
      .dispatch("getMenuList")
      .then((res) => {
        console.log("获取到getMenuList");
        if (res == 1) {
          checkNext();
        } else {
          console.warn("异常，获取不到MenuList");
          toNext()
        }
      })
      .catch((res) => {
        console.warn(res);
      });
  }
  
  function checkNext() {
    if (store.state.allRoute.includes(to.path)) {
      toNext();
    } else {
      console.warn("路径有误，或无权访问:" + to.path);
      next({
        path: "/error404",
        // query: { error: "路径有误，或无权访问", },
        query: { error: "路径有误，或无权访问", redirect: from.path },
      });
    }
  }

  function toNext() {
    if (to.meta.title) {
      store.state.pageTitle = to.meta.title
    }
    next();
  }
});

export default router;
